#include "dominion.h"
#include <stdio.h>
#include "rngs.h"
#include <stdlib.h>
#include <assert.h>
#include<time.h>

int checkSmithy(int prevNum, int currNum) {

  assert(prevNum + 2 == currNum);
}

int main (int argc, char** argv) {
  srand ( time(NULL) );
  FILE * fp;
  fp = fopen("gameResults.out", "w"); 
  for (int i =0; i<20; i++) {
  int randomN = rand() % 1000 + atoi(argv[1]);
  int randomP = rand() % 3 +2;
  struct gameState G = {0};
  int k1 = rand() % 20 + 7;
  int k2 = 0; 
  while (k1 == k2 || k2 < 7) {
    k2 = rand() % 20 + 7;
  }
  int k3 = 0;
  while (k1 == k3 || k2 == k3 || k3 < 7) {
    k3 = rand() % 20 + 7;
  }
  int k4 = 0;
  while (k1 == k4 || k2 == k4 || k3 == k4 || k4 < 7) {
    k4 = rand() % 20 + 7;
  }
  int k5 = 0;
  while (k1 == k5 || k2 == k5 || k3 == k5 || k4 == k5 || k5 < 7) {
    k5 = rand() % 20 + 7;
  }
  int k6 = 0;
  while (k1 == k6 || k2 == k6 || k3 == k6 || k4 == k6 || k5 == k6 || k6 < 7) {
    k6 = rand() % 20 + 7;
  }
  int k7 = 0;
  while (k1 == k7 || k2 == k7 || k3 == k7 || k4 == k7 || k5 == k7 || k6 == k7 || k7 < 7) {
    k7 = rand() % 20 + 7;
  }
  int k8 = 0;
  while (k1 == k8 || k2 == k8 || k3 == k8 || k4 == k8 || k5 == k8 || k6 == k8 || k7 == k8 || k8 < 7) {
    k8 = rand() % 20 + 7;
  }
  int k9 = 0;
  while (k1 == k9 || k2 == k9 || k3 == k9 || k4 == k9 || k5 == k9 || k6 == k9 || k7 == k9 || k8 == k9|| k9 < 7) {
    k9 = rand() % 20 + 7;
  }
  int k10 = 0; 
  while (k1 == k10 || k2 == k10 || k3 == k10 || k4 == k10 || k5 == k10 || k6 == k10 || k7 == k10 || k8 == k10|| k9 == k10 || k10 < 7) {
    k10 = rand() % 20 + 7;
  }
  int k[10] = {k1, k2, k3, k4, k5, k6, k7, 
           k8, k9, k10};
  
  //printf ("Starting game\n");
  fprintf (fp, "Starting game with %d players\n", randomP);
  
  initializeGame(randomP, k, randomN, &G);
  
  int money = 0;
  int smithyPos = -1;
  int adventurerPos = -1;
  int i=0;

  int numSmithies = 0;
  int numAdventurers = 0;

  int num1 = 3;
  int num2 = 3;
  int num3 = 3;
  int num4 = 3;
  int score1 = 0;
  int score2 = 0;
  int score3 = 0;
  int score4 = 0; 

  if (randomP >= 2) {
    fprintf (fp, "The initialized soores for Player 1 is %d and Player 2 is %d", scoreFor(0,&G), scoreFor(1,&G));
    score1 = 3;
    score2 = 3;  
    // assert(scoreFor(0,&G) == 3);
    //assert(scoreFor(1,&G) == 3);
  }
  if (randomP >= 3) {
    fprintf (fp, " and Player 3 is %d", scoreFor(2,&G));
    //assert(scoreFor(2, &G) == 3);
    score3 = 3;
  } 
  if (randomP >= 4) {
    fprintf (fp, " and Player 4 is %d", scoreFor(3,&G));
    //assert(scoreFor(3, &G) == 3);
    score4 = 3;
  }
  fprintf(fp, " and they should be 0:%d 1:%d 2:%d 3:%d\n", scoreFor1(0,&G), scoreFor1(1,&G), scoreFor1(2,&G), scoreFor1(3,&G)); 
  while (!isGameOver(&G)) {
    money = 0;
    smithyPos = -1;
    adventurerPos = -1;
    for (i = 0; i < numHandCards(&G); i++) {
      if (handCard(i, &G) == copper)
    money++;
      else if (handCard(i, &G) == silver)
    money += 2;
      else if (handCard(i, &G) == gold)
    money += 3;
      else if (handCard(i, &G) == smithy)
    smithyPos = i;
      else if (handCard(i, &G) == adventurer)
    adventurerPos = i;
    //printf("%d",smithyPos);
    }

    if (whoseTurn(&G) == 0) {
      //printf("%d",2);
      int scoreCurrent = scoreFor(whoseTurn(&G), &G);
      int scoreCounter = 0;
      if (smithyPos != -1) {
	//printf("0: smithy played from position %d\n", smithyPos);
	//fprintf(fp, "0: smithy played from position %d\n", smithyPos); 
	int prevNum = numHandCards(&G);
        playCard(smithyPos, -1, -1, -1, &G); 
	int currNum = numHandCards(&G);
	fprintf(fp, "0: smithy played from position %d\n", smithyPos);
	checkSmithy(prevNum, currNum);
        //printf("smithy played.\n");
        fprintf(fp,"smithy played.\n");
	money = 0;
	i=0;
        fprintf(fp,"smithy played.\n");
	while(i<numHandCards(&G)){
	  if (handCard(i, &G) == copper){
	    playCard(i, -1, -1, -1, &G);
	    money++;
	  }
	  else if (handCard(i, &G) == silver){
	    playCard(i, -1, -1, -1, &G);
	    money += 2;
	  }
	  else if (handCard(i, &G) == gold){
	    playCard(i, -1, -1, -1, &G);
	    money += 3;
	  }
	  i++;
	}
      }
      if ((&G)->numBuys >= 1) {
      if (money >= 8) {
        int numCoins = (&G)->coins;
	int preSupplyCount = (&G)->supplyCount[province];
	int discardCount = (&G)->discardCount[whoseTurn(&G)];				       
	//printf("0: bought province\n");
	//scoreCounter += 6; 
        buyCard(province, &G);
	if ((preSupplyCount >= 1) && (numCoins >= getCost(province))) {
	scoreCounter+= 6;
	  fprintf(fp, "0: bought province\n");
	  fprintf(fp, "0: The number of coins are %d, it should be %d\n", (&G)->coins, numCoins - getCost(province));
	   fprintf(fp, "0: The number of supply of province are %d, it should be %d\n", (&G)->supplyCount[province], preSupplyCount - 1);
	   fprintf(fp, "0: The number of discards are %d, it should be %d\n", (&G)->discardCount[whoseTurn(&G)], discardCount + 1);
	assert ((numCoins - getCost(province)) == (&G)->coins);
	assert( (&G)->supplyCount[province] == (preSupplyCount -1));
	assert ( (&G)->discardCount[whoseTurn(&G)] == (discardCount + 1));
	  }
	  }            			       
      else if (money >= 6) {
	int numCoins = (&G)->coins;
        int preSupplyCount = (&G)->supplyCount[gold];
        int discardCount = (&G)->discardCount[whoseTurn(&G)];
        //printf("0: bought gold\n"); 
        buyCard(gold, &G);
        if ((preSupplyCount >= 1) && (numCoins >= getCost(gold))) {
	fprintf(fp, "0: bought gold\n");
	  fprintf(fp, "0: The number of coins are %d, it should be %d\n", (&G)->coins, numCoins - getCost(gold));
	   fprintf(fp, "0: The number of supply of gold are %d, it should be %d\n", (&G)->supplyCount[gold], preSupplyCount - 1);
	   fprintf(fp, "0: The number of discards are %d, it should be %d\n", (&G)->discardCount[whoseTurn(&G)], discardCount + 1);

	assert ((numCoins - getCost(gold)) == (&G)->coins);
        assert( (&G)->supplyCount[gold] == (preSupplyCount - 1));
        assert ( (&G)->discardCount[whoseTurn(&G)] == (discardCount + 1));
	  }
      }
      else if ((money >= 4) && (numSmithies < 2)) {
	int numCoins = (&G)->coins;
        int preSupplyCount = (&G)->supplyCount[smithy];
        int discardCount = (&G)->discardCount[whoseTurn(&G)];
	//printf("0: bought smithy\n"); 
        buyCard(smithy, &G);
        numSmithies++;
        //printf("%d\n",numCoins);
	//printf("%d\n",getCost(smithy));
	//printf("%d\n",(&G)->coins);
        if ((preSupplyCount >= 1) && (numCoins >= getCost(smithy))) {
	fprintf(fp, "0: bought smithy\n");
	fprintf(fp, "0: The number of coins are %d, it should be %d\n", (&G)->coins, numCoins - getCost(smithy));
	fprintf(fp, "0: The number of supply of smithy are %d, it should be %d\n", (&G)->supplyCount[smithy], preSupplyCount - 1);
	fprintf(fp, "0: The number of discards are %d, it should be %d\n", (&G)->discardCount[whoseTurn(&G)], discardCount + 1);

	assert ((numCoins - getCost(smithy)) == (&G)->coins);
        assert( (&G)->supplyCount[smithy] == (preSupplyCount - 1));
        assert ( (&G)->discardCount[whoseTurn(&G)] == (discardCount + 1));
	  }
      }
      else if (money >= 3) {
        int numCoins = (&G)->coins;
        int preSupplyCount = (&G)->supplyCount[silver];
        int discardCount = (&G)->discardCount[whoseTurn(&G)];
        //printf("0: bought silver\n"); 
        buyCard(silver, &G);
	if ((preSupplyCount >= 1) && (numCoins >= getCost(silver))) {
	  fprintf(fp, "0: bought silver\n");
	  fprintf(fp, "0: The number of coins are %d, it should be %d\n", (&G)->coins, numCoins - getCost(silver));
	   fprintf(fp, "0: The number of supply of silver are %d, it should be %d\n", (&G)->supplyCount[silver], preSupplyCount - 1);
	   fprintf(fp, "0: The number of discards are %d, it should be %d\n", (&G)->discardCount[whoseTurn(&G)], discardCount + 1);
	assert ((numCoins - getCost(silver)) == (&G)->coins);
        assert( (&G)->supplyCount[silver] == (preSupplyCount - 1));
        assert ( (&G)->discardCount[whoseTurn(&G)] == (discardCount + 1));
	  }
      }
      }
      int newScore = scoreFor(whoseTurn(&G), &G);
      int score1 = score1 + scoreCounter;
      //assert((scoreCurrent + scoreCounter) == newScore);
      //printf("0: end turn\n");
      fprintf(fp, "0: end turn\n");
      endTurn(&G);
    }
    else if (whoseTurn(&G) == 1) {
      int scoreCurrent = scoreFor(whoseTurn(&G), &G);
      int scoreCounter = 0;
      if (adventurerPos != -1) {
        //printf("1: adventurer played from position %d\n", adventurerPos);	
        fprintf(fp, "1: adventurer played from position %d\n", adventurerPos); 
	playCard(adventurerPos, -1, -1, -1, &G);
	int currentPlayer = whoseTurn(&G);
        int drawnTreasure1 = handCard(numHandCards(&G)-1, &G);
        int drawnTreasure2 = handCard(numHandCards(&G)-2, &G);
        assert(drawnTreasure1 == copper || drawnTreasure1 == silver || drawnTreasure1 == gold);
	assert(drawnTreasure2 == copper || drawnTreasure2 == silver || drawnTreasure2 == gold);
        money = 0;
	i=0;
	
        while(i<numHandCards(&G)){
	  if (handCard(i, &G) == copper){
	    playCard(i, -1, -1, -1, &G);
	    money++;         
	  }
	  else if (handCard(i, &G) == silver){
	    playCard(i, -1, -1, -1, &G);
	    money += 2;
	  }
	  else if (handCard(i, &G) == gold){
	    playCard(i, -1, -1, -1, &G);
	    money += 3;
	  }
	  i++;
	}
      }

      if ((&G)->numBuys >= 1) {
      if (money >= 8) {
        int numCoins = (&G)->coins;
	int preSupplyCount = (&G)->supplyCount[province];
	int discardCount = (&G)->discardCount[whoseTurn(&G)];				       
	//printf("0: bought province\n");
	//scoreCounter += 6; 
        buyCard(province, &G);
	if ((preSupplyCount >= 1) && (numCoins >= getCost(province))) {
	scoreCounter+= 6;
	  fprintf(fp, "1: bought province\n");
	  fprintf(fp, "1: The number of coins are %d, it should be %d\n", (&G)->coins, numCoins - getCost(province));
	   fprintf(fp, "1: The number of supply of province are %d, it should be %d\n", (&G)->supplyCount[province], preSupplyCount - 1);
	   fprintf(fp, "1: The number of discards are %d, it should be %d\n", (&G)->discardCount[whoseTurn(&G)], discardCount + 1);
	assert ((numCoins - getCost(province)) == (&G)->coins);
	assert( (&G)->supplyCount[province] == (preSupplyCount -1));
	assert ( (&G)->discardCount[whoseTurn(&G)] == (discardCount + 1));
	  }
	  }
      else if ((money >= 6) && (numAdventurers < 2)) {
	int numCoins = (&G)->coins;
        int preSupplyCount = (&G)->supplyCount[adventurer];
        int discardCount = (&G)->discardCount[whoseTurn(&G)];
	//printf("0: bought smithy\n"); 
        buyCard(adventurer, &G);
        numAdventurers++;
        //printf("%d\n",numCoins);
	//printf("%d\n",getCost(smithy));
	//printf("%d\n",(&G)->coins);
        if ((preSupplyCount >= 1) && (numCoins >= getCost(adventurer))) {
	fprintf(fp, "1: bought adventurer\n");
	fprintf(fp, "1: The number of coins are %d, it should be %d\n", (&G)->coins, numCoins - getCost(adventurer));
	fprintf(fp, "1: The number of supply of adventurer are %d, it should be %d\n", (&G)->supplyCount[adventurer], preSupplyCount - 1);
	fprintf(fp, "1: The number of discards are %d, it should be %d\n", (&G)->discardCount[whoseTurn(&G)], discardCount + 1);

	assert ((numCoins - getCost(adventurer)) == (&G)->coins);
        assert( (&G)->supplyCount[adventurer] == (preSupplyCount - 1));
        assert ( (&G)->discardCount[whoseTurn(&G)] == (discardCount + 1));
	  }
      }            			       
      else if (money >= 6) {
	int numCoins = (&G)->coins;
        int preSupplyCount = (&G)->supplyCount[gold];
        int discardCount = (&G)->discardCount[whoseTurn(&G)];
        //printf("0: bought gold\n"); 
        buyCard(gold, &G);
        if ((preSupplyCount >= 1) && (numCoins >= getCost(gold))) {
	fprintf(fp, "1: bought gold\n");
	  fprintf(fp, "1: The number of coins are %d, it should be %d\n", (&G)->coins, numCoins - getCost(gold));
	   fprintf(fp, "1: The number of supply of gold are %d, it should be %d\n", (&G)->supplyCount[gold], preSupplyCount - 1);
	   fprintf(fp, "1: The number of discards are %d, it should be %d\n", (&G)->discardCount[whoseTurn(&G)], discardCount + 1);

	assert ((numCoins - getCost(gold)) == (&G)->coins);
        assert( (&G)->supplyCount[gold] == (preSupplyCount - 1));
        assert ( (&G)->discardCount[whoseTurn(&G)] == (discardCount + 1));
	  }
      }
      else if (money >= 3) {
        int numCoins = (&G)->coins;
        int preSupplyCount = (&G)->supplyCount[silver];
        int discardCount = (&G)->discardCount[whoseTurn(&G)];
        //printf("0: bought silver\n"); 
        buyCard(silver, &G);
	if ((preSupplyCount >= 1) && (numCoins >= getCost(silver))) {
	  fprintf(fp, "1: bought silver\n");
	  fprintf(fp, "1: The number of coins are %d, it should be %d\n", (&G)->coins, numCoins - getCost(silver));
	   fprintf(fp, "1: The number of supply of silver are %d, it should be %d\n", (&G)->supplyCount[silver], preSupplyCount - 1);
	   fprintf(fp, "1: The number of discards are %d, it should be %d\n", (&G)->discardCount[whoseTurn(&G)], discardCount + 1);
	assert ((numCoins - getCost(silver)) == (&G)->coins);
        assert( (&G)->supplyCount[silver] == (preSupplyCount - 1));
        assert ( (&G)->discardCount[whoseTurn(&G)] == (discardCount + 1));
	  }
      }
      }
      int newScore = scoreFor(whoseTurn(&G), &G);
      score2 = score2 + scoreCounter;
      //assert((scoreCurrent + scoreCounter) == newScore);
      //printf("0: end turn\n");
      fprintf(fp, "1: end turn\n");
      endTurn(&G);
    }
  else if (whoseTurn(&G) == 2) {
      int scoreCurrent = scoreFor(whoseTurn(&G), &G);
      int scoreCounter = 0;
if (smithyPos != -1) {
  //printf("0: smithy played from position %d\n", smithyPos);
        fprintf(fp, "2: smithy played from position %d\n", smithyPos);
	int prevNum = numHandCards(&G);
        playCard(smithyPos, -1, -1, -1, &G); 
	int currNum = numHandCards(&G);
	checkSmithy(prevNum, currNum);
	fprintf(fp, "smithy played.\n");
	//printf("smithy played.\n");
	money = 0;
	i=0;
	while(i<numHandCards(&G)){
	  if (handCard(i, &G) == copper){
	    playCard(i, -1, -1, -1, &G);
	    money++;
	  }
	  else if (handCard(i, &G) == silver){
	    playCard(i, -1, -1, -1, &G);
	    money += 2;
	  }
	  else if (handCard(i, &G) == gold){
	    playCard(i, -1, -1, -1, &G);
	    money += 3;
	  }
	  i++;
	}
      }

      if ((&G)->numBuys >= 1) {
      if (money >= 8) {
        int numCoins = (&G)->coins;
	int preSupplyCount = (&G)->supplyCount[province];
	int discardCount = (&G)->discardCount[whoseTurn(&G)];				       
	//printf("0: bought province\n");
	//scoreCounter += 6; 
        buyCard(province, &G);
	if ((preSupplyCount >= 1) && (numCoins >= getCost(province))) {
	scoreCounter+= 6;
	  fprintf(fp, "2: bought province\n");
	  fprintf(fp, "2: The number of coins are %d, it should be %d\n", (&G)->coins, numCoins - getCost(province));
	   fprintf(fp, "2: The number of supply of province are %d, it should be %d\n", (&G)->supplyCount[province], preSupplyCount - 1);
	   fprintf(fp, "2: The number of discards are %d, it should be %d\n", (&G)->discardCount[whoseTurn(&G)], discardCount + 1);
	assert ((numCoins - getCost(province)) == (&G)->coins);
	assert( (&G)->supplyCount[province] == (preSupplyCount -1));
	assert ( (&G)->discardCount[whoseTurn(&G)] == (discardCount + 1));
	  }
	  }            			       
      else if (money >= 6) {
	int numCoins = (&G)->coins;
        int preSupplyCount = (&G)->supplyCount[gold];
        int discardCount = (&G)->discardCount[whoseTurn(&G)];
        //printf("0: bought gold\n"); 
        buyCard(gold, &G);
        if ((preSupplyCount >= 1) && (numCoins >= getCost(gold))) {
	fprintf(fp, "2: bought gold\n");
	  fprintf(fp, "2: The number of coins are %d, it should be %d\n", (&G)->coins, numCoins - getCost(gold));
	   fprintf(fp, "2: The number of supply of gold are %d, it should be %d\n", (&G)->supplyCount[gold], preSupplyCount - 1);
	   fprintf(fp, "2: The number of discards are %d, it should be %d\n", (&G)->discardCount[whoseTurn(&G)], discardCount + 1);

	assert ((numCoins - getCost(gold)) == (&G)->coins);
        assert( (&G)->supplyCount[gold] == (preSupplyCount - 1));
        assert ( (&G)->discardCount[whoseTurn(&G)] == (discardCount + 1));
	  }
      }
      else if ((money >= 4) && (numSmithies < 2)) {
	int numCoins = (&G)->coins;
        int preSupplyCount = (&G)->supplyCount[smithy];
        int discardCount = (&G)->discardCount[whoseTurn(&G)];
	//printf("0: bought smithy\n"); 
        buyCard(smithy, &G);
        numSmithies++;
        //printf("%d\n",numCoins);
	//printf("%d\n",getCost(smithy));
	//printf("%d\n",(&G)->coins);
        if ((preSupplyCount >= 1) && (numCoins >= getCost(smithy))) {
	fprintf(fp, "2: bought smithy\n");
	fprintf(fp, "2: The number of coins are %d, it should be %d\n", (&G)->coins, numCoins - getCost(smithy));
	fprintf(fp, "2: The number of supply of smithy are %d, it should be %d\n", (&G)->supplyCount[smithy], preSupplyCount - 1);
	fprintf(fp, "2: The number of discards are %d, it should be %d\n", (&G)->discardCount[whoseTurn(&G)], discardCount + 1);

	assert ((numCoins - getCost(smithy)) == (&G)->coins);
        assert( (&G)->supplyCount[smithy] == (preSupplyCount - 1));
        assert ( (&G)->discardCount[whoseTurn(&G)] == (discardCount + 1));
	  }
      }
      else if (money >= 3) {
        int numCoins = (&G)->coins;
        int preSupplyCount = (&G)->supplyCount[silver];
        int discardCount = (&G)->discardCount[whoseTurn(&G)];
        //printf("0: bought silver\n"); 
        buyCard(silver, &G);
	if ((preSupplyCount >= 1) && (numCoins >= getCost(silver))) {
	  fprintf(fp, "2: bought silver\n");
	  fprintf(fp, "2: The number of coins are %d, it should be %d\n", (&G)->coins, numCoins - getCost(silver));
	   fprintf(fp, "2: The number of supply of silver are %d, it should be %d\n", (&G)->supplyCount[silver], preSupplyCount - 1);
	   fprintf(fp, "2: The number of discards are %d, it should be %d\n", (&G)->discardCount[whoseTurn(&G)], discardCount + 1);
	assert ((numCoins - getCost(silver)) == (&G)->coins);
        assert( (&G)->supplyCount[silver] == (preSupplyCount - 1));
        assert ( (&G)->discardCount[whoseTurn(&G)] == (discardCount + 1));
	  }
      }
      }
      int newScore = scoreFor(whoseTurn(&G), &G);
      score3 = score3 + scoreCounter;
      //assert((scoreCurrent + scoreCounter) == newScore);
      //printf("0: end turn\n");
      fprintf(fp, "2: end turn\n");
      endTurn(&G);
}
else if (whoseTurn(&G) ==3) {
      int scoreCurrent = scoreFor(whoseTurn(&G), &G);
      int scoreCounter = 0;
if (adventurerPos != -1) {
  //printf("3: adventurer played from position %d\n", adventurerPos);	
	fprintf(fp, "3: adventurer played from position %d\n", adventurerPos);
	playCard(adventurerPos, -1, -1, -1, &G);
	int currentPlayer = whoseTurn(&G);
        int drawnTreasure1 = handCard(numHandCards(&G)-1, &G);
        int drawnTreasure2 = handCard(numHandCards(&G)-2, &G);
        assert(drawnTreasure1 == copper || drawnTreasure1 == silver || drawnTreasure1 == gold);
	assert(drawnTreasure2 == copper || drawnTreasure2 == silver || drawnTreasure2 == gold);
        money = 0;
	i=0;
	
        while(i<numHandCards(&G)){
	  if (handCard(i, &G) == copper){
	    playCard(i, -1, -1, -1, &G);
	    money++;         
	  }
	  else if (handCard(i, &G) == silver){
	    playCard(i, -1, -1, -1, &G);
	    money += 2;
	  }
	  else if (handCard(i, &G) == gold){
	    playCard(i, -1, -1, -1, &G);
	    money += 3;
	  }
	  i++;
	}
      }

      if ((&G)->numBuys >= 1) {
      if (money >= 8) {
        int numCoins = (&G)->coins;
	int preSupplyCount = (&G)->supplyCount[province];
	int discardCount = (&G)->discardCount[whoseTurn(&G)];				       
	//printf("0: bought province\n");
	//scoreCounter += 6; 
        buyCard(province, &G);
	if ((preSupplyCount >= 1) && (numCoins >= getCost(province))) {
	scoreCounter+= 6;
	  fprintf(fp, "3: bought province\n");
	  fprintf(fp, "3: The number of coins are %d, it should be %d\n", (&G)->coins, numCoins - getCost(province));
	   fprintf(fp, "3: The number of supply of province are %d, it should be %d\n", (&G)->supplyCount[province], preSupplyCount - 1);
	   fprintf(fp, "3: The number of discards are %d, it should be %d\n", (&G)->discardCount[whoseTurn(&G)], discardCount + 1);
	assert ((numCoins - getCost(province)) == (&G)->coins);
	assert( (&G)->supplyCount[province] == (preSupplyCount -1));
	assert ( (&G)->discardCount[whoseTurn(&G)] == (discardCount + 1));
	  }
	  }
else if ((money >= 6) && (numAdventurers < 2)) {
	int numCoins = (&G)->coins;
        int preSupplyCount = (&G)->supplyCount[adventurer];
        int discardCount = (&G)->discardCount[whoseTurn(&G)];
	//printf("0: bought smithy\n"); 
        buyCard(adventurer, &G);
        numAdventurers++;
        //printf("%d\n",numCoins);
	//printf("%d\n",getCost(smithy));
	//printf("%d\n",(&G)->coins);
        if ((preSupplyCount >= 1) && (numCoins >= getCost(adventurer))) {
	fprintf(fp, "3: bought adventurer\n");
	fprintf(fp, "3: The number of coins are %d, it should be %d\n", (&G)->coins, numCoins - getCost(adventurer));
	fprintf(fp, "3: The number of supply of adventurer are %d, it should be %d\n", (&G)->supplyCount[adventurer], preSupplyCount - 1);
	fprintf(fp, "3: The number of discards are %d, it should be %d\n", (&G)->discardCount[whoseTurn(&G)], discardCount + 1);

	assert ((numCoins - getCost(adventurer)) == (&G)->coins);
        assert( (&G)->supplyCount[adventurer] == (preSupplyCount - 1));
        assert ( (&G)->discardCount[whoseTurn(&G)] == (discardCount + 1));
	  }
      }            	            			       
      else if (money >= 6) {
	int numCoins = (&G)->coins;
        int preSupplyCount = (&G)->supplyCount[gold];
        int discardCount = (&G)->discardCount[whoseTurn(&G)];
        //printf("0: bought gold\n"); 
        buyCard(gold, &G);
        if ((preSupplyCount >= 1) && (numCoins >= getCost(gold))) {
	fprintf(fp, "3: bought gold\n");
	  fprintf(fp, "3: The number of coins are %d, it should be %d\n", (&G)->coins, numCoins - getCost(gold));
	   fprintf(fp, "3: The number of supply of gold are %d, it should be %d\n", (&G)->supplyCount[gold], preSupplyCount - 1);
	   fprintf(fp, "3: The number of discards are %d, it should be %d\n", (&G)->discardCount[whoseTurn(&G)], discardCount + 1);

	assert ((numCoins - getCost(gold)) == (&G)->coins);
        assert( (&G)->supplyCount[gold] == (preSupplyCount - 1));
        assert ( (&G)->discardCount[whoseTurn(&G)] == (discardCount + 1));
	  }
      }
      else if (money >= 3) {
        int numCoins = (&G)->coins;
        int preSupplyCount = (&G)->supplyCount[silver];
        int discardCount = (&G)->discardCount[whoseTurn(&G)];
        //printf("0: bought silver\n"); 
        buyCard(silver, &G);
	if ((preSupplyCount >= 1) && (numCoins >= getCost(silver))) {
	  fprintf(fp, "3: bought silver\n");
	  fprintf(fp, "3: The number of coins are %d, it should be %d\n", (&G)->coins, numCoins - getCost(silver));
	   fprintf(fp, "3: The number of supply of silver are %d, it should be %d\n", (&G)->supplyCount[silver], preSupplyCount - 1);
	   fprintf(fp, "3: The number of discards are %d, it should be %d\n", (&G)->discardCount[whoseTurn(&G)], discardCount + 1);
	assert ((numCoins - getCost(silver)) == (&G)->coins);
        assert( (&G)->supplyCount[silver] == (preSupplyCount - 1));
        assert ( (&G)->discardCount[whoseTurn(&G)] == (discardCount + 1));
	  }
      }
      }
      int newScore = scoreFor(whoseTurn(&G), &G);
      //assert((scoreCurrent + scoreCounter) == newScore);
      //printf("0: end turn\n");
      score4 = score4 + scoreCounter;
      fprintf(fp, "0: end turn\n");
      endTurn(&G);


}
  } // end of While

  //printf ("Finished game.\n");
  fprintf (fp, "Finished game.\n");
  /*if (randomP == 0) {
  printf ("Player 0: %d\n", scoreFor(0, &G));

  }*/
  if (randomP == 2) {
    //printf ("Player 0: %d\nPlayer 1: %d\n", scoreFor(0, &G), scoreFor(1, &G));
 fprintf (fp, "Player 0: %d\nPlayer 1: %d\n and it should be %d and %d" , scoreFor(0, &G), scoreFor(1, &G), scoreFor1(0, &G), scoreFor1(0,&G), scoreFor1(1, &G));
  }
  if (randomP == 3) {
    //printf ("Player 0: %d\nPlayer 1: %d\nPlayer 2: %d\n and it shoudl be %d,%d, and %d\n", scoreFor(0, &G), scoreFor(1, &G), scoreFor(2, &G), scoreFor1(0,&G), scoreFor1(1, &G), scoreFor1(2, &G));
    fprintf (fp, "Player 0: %d\nPlayer 1: %d\nPlayer 2: %d\n and it should be %d, %d, and %d\n", scoreFor(0, &G), scoreFor(1, &G), scoreFor(2, &G), scoreFor1(0, &G), scoreFor1(1, &G), scoreFor1(2, &G));
  }
  if (randomP == 4) {
    //printf ("Player 0: %d\nPlayer 1: %d\nPlayer 2: %d\nPlayer 3: %d\n", scoreFor(0, &G), scoreFor(1, &G), scoreFor(2, &G), scoreFor(3, &G));
    fprintf (fp, "Player 0: %d\nPlayer 1: %d\nPlayer 2: %d\nPlayer 3: %d and it should be %d, %d, %d, and %d\n", scoreFor(0, &G), scoreFor(1, &G), scoreFor(2, &G), scoreFor(3, &G), scoreFor1(0, &G), scoreFor1(1, &G), scoreFor1(2, &G), scoreFor1(3, &G));

  }
  }
  int randomPass = rand() %2;
  FILE * newFile;
  newFile = fopen("passCheck.txt", "w");
  if (randomPass == 0) {
    fprintf(newFile, "1");
    printf(" %d ", randomPass);
  }
  if (randomPass == 1) {
    fprintf(newFile, "2");
    printf(" %d ", randomPass);
  }
  fclose(fp);
  fclose(newFile);
  return 0;
}
